/**
* 查看题库
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>查看题库</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="testData" class="user-search">
      <el-form-item label="">
        <el-input  v-model="testData.testName" placeholder="试题名称"></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-input  v-model="testData.updatePeople" placeholder="上传人"></el-input>
      </el-form-item>
      <el-select  v-model="testData.isMultilychoice" clearable filterable placeholder="单选多选">
        <el-option
          v-for="item in statusOptions"
          :key="item.dataId"
          :label="item.dataName"
          :value="item.dataId">
        </el-option>
      </el-select>
      <el-select  v-model="testData.difficulty" clearable filterable placeholder="难度">
        <el-option
          v-for="item in selectdata"
          :key="item.dataId"
          :label="item.dataName"
          :value="item.dataId">
        </el-option> 
      </el-select>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" @selection-change="selectChange" :data="userData" highlight-current-row v-loading="loading"
              border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" sortable prop="testName" label="试题名称" width="220">
      </el-table-column>
      <el-table-column align="center" sortable prop="updatePeople" label="上传人" width="220">
      </el-table-column>
      <el-table-column align="center" sortable prop="isMultilychoice" label="单选多选" width="220">
      </el-table-column>
      <el-table-column align="center" sortable prop="difficulty" label="难度" width="220">
      </el-table-column>
      <el-table-column label="操作" min-width="300">
        <template slot-scope="scope">
      
          <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:childMsg="pageparm" @callFather="callFather"></Pagination>
  </div>
</template>

<script>
// 导入请求方法
import {
  userList,
  userSave,
  userDelete,
  userPwd,
  userExpireToken,
  userFlashCache,
  userLock,
  UserDeptTree,
  UserDeptSave,
  UserDeptdeptTree,
  UserChangeDept
} from '../../api/userMG'
import Pagination from '../../components/Pagination'

export default {
  data() {
    return {
    
      loading: false, //是显示加载
      // 选择数据
      selectdata: [],
      // 删除用户
      seletedata: {
        ids: '',
        token: localStorage.getItem('logintoken')
      },
      // 重置密码
      resetpsd: {
        userId: '',
        token: localStorage.getItem('logintoken')
      },
      // 用户下线
      offline: {
        token: localStorage.getItem('logintoken')
      },
      // 请求数据参数
      formInline: {
        page: 1,
        limit: 10,
      }
      ,
      testData: {
        testName: '',
        isMultilychoice: '',
        answerIds: '',
        difficulty: '',
        trainId:'',
        updatePeople:""
      },
      //用户数据
      userData: [],
      // 数据权限
      UserDept: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 选中
      checkmenu: [],
      //参数role
      saveroleId: '',
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 5,
        total: 10
      },
      //单选多选选择参数
      isMultilychoiceOptions : []
    }
  },
  // 注册组件
  components: {
    Pagination
  },

  /**
   * 数据发生改变
   */
  watch: {},

  /**
   * 创建完毕
   */
  created() {
    this.getdata(this.formInline)
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取数据方法
    getdata(parameter) {
      this.loading = true
      /***
       * 调用接口，注释上面模拟数据 取消下面注释
       */
      this.pageparm.currentPage = this.formInline.page
      this.pageparm.pageSize = this.formInline.limit
      // 获取试题列表
      this.$axios.post('/api/test/findTest/' + this.pageparm.currentPage + '/' + this.pageparm.pageSize, this.testData)
        .then(res => {
          console.log(res)
          this.loading = false
          if (res.data.code === 200) {
            this.$message({
              type: 'info',
              message: res.data.message
            })
            this.userData = res.data.data.list
            this.pageparm.total = res.data.data.total
          }
        })
       this.$axios.get('/api/test/findBothStatus').then(res =>{
        console.log(res);
        if (res.data.code === 200){
          this.selectChange(res.data.data)
        }
      })
      
      
    },
    // 分页插件事件
    callFather(parm) {
      console.log(parm)
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata(this.formInline)
    },
    //搜索事件
    search() {
      this.getdata(this.formInline)
    },
    //显示编辑界面
        //显示编辑界面
    handleEdit: function (index, row) {
      this.$confirm('是否编辑?', '提示', {
        confirmButtonText: '编辑',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$router.push({name:'addTest',params:{id: row.id}})
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消编辑'
        });
      });
    },
   
    // 选择复选框事件
    selectChange(val) {
      this.selectdata = val[0]
      this.statusOptions = val[1]
    },
    
    // 删除用户
    deleteUser(index, row) {
      console.log(index,row)
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {

          // 删除试题
          this.$axios.delete('/api/test/deleteById/'+row.id)
            .then(res => {
              if (res.data.code === 200) {
                this.$message({
                  type: 'success',
                  message: '数据已删除!'
                })
                this.getdata(this.formInline)
              } else {
                this.$message({
                  type: 'info',
                  message: res.data.message
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('数据删除失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除！'
          })
        })
    },
    
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}

.userRole {
  width: 100%;
}
</style>

